/* 
 * $Id: edit.h 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
 *
 * Copyright (c) 2006, 2007, 2008 Prabhu Gurumurthy <pgurumur@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef __PY_EDIT_H_
#define __PY_EDIT_H_

/*
 * Essential includes
 */
#include <sys/types.h>

#include <fcntl.h>
#include <histedit.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/*
 * Python specific includes
 */
#include <Python.h>
#include <listobject.h>
#include <structmember.h>

#define MAXBUF 4096
#define DEFAULT_HISTSIZE 100
#define DEFAULT_EDITOR "emacs"

/*
 * Bunch of defines for alloc, free
 */
#define EL_MEMBER(object) struct PyMemberDef object
#define EL_MEMBER_LIST(a,b,c,d,e) { a, b, c, d, e }
#define EL_ALLOC(object, type) (object *)type.tp_alloc(&type, 0)
#define EL_FREE(object) object->ob_type->tp_free((PyObject *)ob)

/*
 * Main editline python object
 */
typedef struct {
   PyObject_HEAD
   PyObject *converter;
   PyObject *list;
   EditLine *edit;
   History *hist;
   HistEvent hev;
   const char *histfile;
   char *prompt;
} PyEdit_Object;

/* Error object */
static PyObject *PyEdit_Error;

/* Editline object type */
extern PyTypeObject PyEdit_ObjectType;

/* version, default prompt and minimalistic documentation */
const char *_PyEdit_Version = "0.1";
static char _PyEdit_Doc[] = "editline in python";

/* My init function */
void initeditline(void);

/* Module init specifi function for alloc, dealloc, get and set attributes */
static void _PyEdit_Object_DeAlloc(PyEdit_Object *);
static int _PyEdit_Traverse(PyEdit_Object *, visitproc, void *);
static int _PyEdit_SetAttr(PyEdit_Object *, char *, PyObject *);
static PyObject *_PyEdit_GetAttr(PyEdit_Object *, char *);
static PyObject *PyEdit_Delete(PyEdit_Object *, PyObject *);

/* method functions */
static PyObject *PyEdit_Init(PyObject *, PyObject *, PyObject *);
static PyObject *PyEdit_Gets(PyEdit_Object *, PyObject *);
static PyObject *PyEdit_History(PyEdit_Object *, PyObject *);
static PyObject *PyEdit_SetCompleter(PyEdit_Object *, PyObject *);

/* utility function */
static char *get_edit_prompt(EditLine *);
static unsigned char PyEdit_Complete(EditLine *, int);

/* What are the functions that this module provides? */
static PyMethodDef PyEdit_Methods[] = {
   {  /* called using editline.init() */
      "init",   
      (PyCFunction)PyEdit_Init, 
      METH_VARARGS | METH_KEYWORDS, 
      NULL 
   },
   { /* called using object.gets() */
      "gets",   
      (PyCFunction)PyEdit_Gets, 
      METH_VARARGS, 
      NULL 
   },
   { /* called using object.history() */
      "history", 
      (PyCFunction)PyEdit_History, 
      METH_VARARGS, 
      NULL 
   },
   { /* object.completer() */
      "completer",
      (PyCFunction)PyEdit_SetCompleter,
      METH_VARARGS,
      NULL
   },
   { NULL, NULL, 0, NULL },
};

static EL_MEMBER(_PyEdit_ObjectMemberList)[] = {
   EL_MEMBER_LIST("converter", T_OBJECT, offsetof(PyEdit_Object, converter), RO,
         "Type conversion mapping"),
   { NULL, 0, 0, 0, NULL }
};

#endif /* __PY_EDIT_H_ */
